package com.fantastic.web.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeAppPayRequest;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeAppPayResponse;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.fantastic.common.exception.AlipayException;
import com.fantastic.common.exception.enums.RespStatusInfo;
import com.fantastic.model.vo.zjs.OrderVo;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * @author: rxx
 * @since: 2024/6/20 09:25
 * @description: 真正支付的类
 */
public class AlipayPay {
    /**
     * web端支付
     * sendRequestToAlipay方法设置参数，向支付宝发送请求，里面这些参数约束是支付宝规定好的。
     * sendRequestToAlipay中的这个pageExecute是向支付宝发请求。
     *
     * @return
     * @throws AlipayApiException
     */
    public static String pageAlipayPay(AlipayClient alipayClient, AlipayTradePagePayRequest alipayPageRequest,
                                       OrderVo orders) {
        //生成GSON字符串
        AlipayPayUtil.setPageBizContent(alipayPageRequest, orders);
        //发起支付请求，
        AlipayTradePagePayResponse response = (AlipayTradePagePayResponse) AlipayPayUtil.pageExecute(alipayClient, alipayPageRequest);
        //返回gson支付对象
        return response.getBody();
    }

    /**
     * app支付
     *
     * @param orders        订单对象
     * @param alipayClient  支付宝客户端对象
     * @param appPayRequest app支付请求对象
     * @return 返回支付宝特定的GSON字符串，交由前端进行解析（app端：如uniApp，解析方式查看具体的文档）
     */
    public static void appAlipayPay(HttpServletResponse resp, AlipayClient alipayClient, AlipayTradeAppPayRequest appPayRequest,
                                    OrderVo orders) {
        //1、填充请求参数
        AlipayPayUtil.setAppBizContent(appPayRequest, orders);
        //2、 执行请求后返回响应对象
        AlipayTradeAppPayResponse response = (AlipayTradeAppPayResponse) AlipayPayUtil.pageExecute(alipayClient, appPayRequest);

        //3、检查是否成功调用支付宝接口
        AlipayPayUtil.isAppSuccess(response);
        println(resp, response.getBody());
    }

    /**
     * wap支付
     *
     * @param orders        订单对象
     * @param alipayClient  支付宝客户端对象
     * @param wapPayRequest wap支付请求对象
     * @return 返回支付宝特定的GSON字符串，交由前端进行解析（app端：如uniApp，解析方式查看具体的文档）
     */
    public static void wapAlipayPay(HttpServletResponse resp, AlipayClient alipayClient, AlipayTradeWapPayRequest wapPayRequest,
                                    OrderVo orders) {
        //1、填充请求参数
        AlipayPayUtil.setWapBizContent(wapPayRequest, orders);
        //2、 执行请求后返回响应对象
        AlipayTradeWapPayResponse response = (AlipayTradeWapPayResponse) AlipayPayUtil.pageExecute(alipayClient, wapPayRequest);
        //3、检查是否成功调用支付宝接口
        AlipayPayUtil.isWapSuccess(response);
        //4、返回生成的字符串
        println(resp, response.getBody());
    }

    /**
     * 响应方法
     *
     * @param resp
     * @param body
     */
    private static void println(HttpServletResponse resp, String body) {
        try {
            resp.setContentType("text/html;charset=utf-8");
            resp.getWriter().println(body);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 返回充值后的账号余额
     *
     * @param accountStored 账号原有的金额
     * @param topMoney      充值的金额
     * @return
     */
    public static BigDecimal add(BigDecimal accountStored, BigDecimal topMoney) {
        //查看账号金额是否足够赠送金额
        BigDecimal bigDecimal = giveMoney(topMoney);
        return accountStored.add(bigDecimal);
    }

    /**
     * 账号金额
     *
     * @param topMoney 用户充值的金额
     * @return
     */
    private static BigDecimal giveMoney(BigDecimal topMoney) {
        //51和100之间+10  大于0表示前者大于后者
        if (topMoney.compareTo(Money.num1.getMBorder()) > 0 &&
                topMoney.compareTo(Money.num2.getMBorder()) < 0) {
            return new BigDecimal(String.valueOf(topMoney.add(Money.money1.getMBorder())));
        } else if (topMoney.compareTo(Money.num3.getMBorder()) > 0 &&
                topMoney.compareTo(Money.num4.getMBorder()) < 0) {
            return new BigDecimal(String.valueOf(topMoney.add(Money.money2.getMBorder())));
        } else if (topMoney.compareTo(Money.num5.getMBorder()) > 0 &&
                topMoney.compareTo(Money.num6.getMBorder()) < 0) {
            return new BigDecimal(String.valueOf(topMoney.add(Money.money3.getMBorder())));
        } else if (topMoney.compareTo(Money.num7.getMBorder()) > 0 &&
                topMoney.compareTo(Money.num8.getMBorder()) < 0) {
            return new BigDecimal(String.valueOf(topMoney.add(Money.money4.getMBorder())));
        } else if (topMoney.compareTo(Money.num9.getMBorder()) > 0) {
            return new BigDecimal(String.valueOf(topMoney.add(Money.money5.getMBorder())));
        }
        return topMoney;
    }

    /**
     * 返回购买商品后的账号余额
     *
     * @param accountStored 账号原有的金额
     * @param consumeMoney  消费的金额
     * @return
     */
    public static BigDecimal subtract(BigDecimal accountStored, BigDecimal consumeMoney) {
        //余额校验
        moneyCheck(accountStored, consumeMoney);
        return accountStored.subtract(consumeMoney);
    }

    /**
     * 判断余额是否足够消费
     *
     * @param accountStored 账号原有的金额
     * @param consumeMoney  消费的金额
     */
    private static void moneyCheck(BigDecimal accountStored, BigDecimal consumeMoney) {
        int i = accountStored.compareTo(consumeMoney);
        //小于零，表示后者大于前者
        if (i < 0) {
            throw new AlipayException(RespStatusInfo.MONEY_NOT);
        }
    }


}
